package applications.studentApplicationForm.ui.lib;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.util.Observable;

import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;


import org.objectweb.fractal.fraclet.annotations.Attribute;
import org.objectweb.fractal.fraclet.annotations.Component;
import org.objectweb.fractal.fraclet.annotations.Interface;
import org.objectweb.fractal.fraclet.annotations.Requires;

import applications.studentApplicationForm.fc.api.StudentForm;

@Component(provides = {@Interface(name = "jPanelItf", signature = org.objectweb.fractal.swing.JPanelItf.class), @Interface(name = "jPanelAttributes", signature = org.objectweb.fractal.swing.JPanelAttributes.class), @Interface(name = "uicontainerservice", signature = ontocompo.api.UIContainerService.class), @Interface(name = "ontocompointrospection", signature = ontocompo.api.OntoCompoIntrospection.class)})


// ListSelectionListener,
public class StudentApplicationFormYearPanel extends JPanel  implements   java.util.Observer, org.objectweb.fractal.swing.JPanelItf, org.objectweb.fractal.swing.JPanelAttributes, ontocompo.api.UIContainerService, ontocompo.api.OntoCompoIntrospection {

	private String uid;
	private org.objectweb.fractal.api.Component c;
	private JTable table;

	private String valeur;
	
	@Attribute
	private int widthCell;
	
	@Attribute
	private int heightCell;
	
	@Requires(name="pane")
	private PanelInterface mypane;
	
	@Requires(name="yearyeartitle")
	private JLabelInterface yearyeartitle;
	
	@Requires(name="yearcodetitle")
	private JLabelInterface yearcodetitle;
	
	@Requires(name="yearunivtitle")
	private JLabelInterface yearunivtitle;

	@Requires(name="yeargradetitle")
	private JLabelInterface yeargradetitle;

	@Requires(name="yearyear")
	private PanelColInterface yearyear;
	
	@Requires(name="yearcode")
	private PanelColInterface yearcode;
	
	@Requires(name="yearuniv")
	private PanelColInterface yearuniv;

	@Requires(name="yeargrade")
	private PanelColInterface yeargrade;


	@Requires(name = "studentformdirectoryservice")
	private applications.studentApplicationForm.fc.api.StudentFormDirectoryService studentformdirectoryservice;

	@Requires(name = "studentformdirectoryobservable")
	private ontocompo.api.Observable studentformdirectoryobservable;


	public StudentApplicationFormYearPanel() {
		super();
		this.uid = "applications.studentApplicationForm.ui.lib.StudentApplicationFormYearPanel";
		System.out.println(this.toString() + " created");
		this.valeur = valeur;
	}


	@Override
	public void init() {
		// TODO Auto-generated method stub

		this.studentformdirectoryobservable.addObserver(this);

		this.setLayout(new BorderLayout());
		this.setBackground(Color.white);



		this.add("North", createTh("Subjects"));

		System.out.println("coucou -1 "+widthCell+" "+ heightCell);
		this.add("Center", mypane.getComponentItSelf());
		mypane.init();
		mypane.setLayout(new BoxLayout(mypane.getComponentItSelf(), BoxLayout.Y_AXIS));
		Dimension dim = new Dimension((widthCell+yearyear.getMarge()*2)*4, heightCell*6+yearyear.getMarge()*2);

		modifyComponent(mypane.getComponentItSelf(), dim);
		
		
		JPanel titres = new JPanel();
		titres.setLayout(new BoxLayout(titres, BoxLayout.X_AXIS));
		Dimension d_titres = new Dimension((widthCell+yearyear.getMarge()*2)*4, heightCell);
		modifyComponent(titres, d_titres);
		
		Dimension d_elements = new Dimension(widthCell+yearyear.getMarge()*2, heightCell);
		modifyComponent(yearyeartitle.getComponentItSelf(), d_elements);
		titres.add(yearyeartitle.getComponentItSelf());
		
		modifyComponent(yearcodetitle.getComponentItSelf(), d_elements);
		titres.add(yearcodetitle.getComponentItSelf());
		
		modifyComponent(yearunivtitle.getComponentItSelf(), d_elements);
		titres.add(yearunivtitle.getComponentItSelf());
		
		modifyComponent(yeargradetitle.getComponentItSelf(), d_elements);
		titres.add(yeargradetitle.getComponentItSelf());
		
		mypane.add(titres);
		
		JPanel colonnes = new JPanel();
		Dimension d_col = new Dimension((widthCell+yearcode.getMarge()*2)*4, heightCell*5+yearcode.getMarge()*2);
		modifyComponent(colonnes, d_col);

		colonnes.setLayout(new BoxLayout(colonnes, BoxLayout.X_AXIS));
		yearyear.init();
		colonnes.add(yearyear.getComponentItSelf());
		yearcode.init();
		colonnes.add(yearcode.getComponentItSelf());
		yearuniv.init();
		colonnes.add(yearuniv.getComponentItSelf());
		yeargrade.init();
		colonnes.add(yeargrade.getComponentItSelf());
		
		mypane.add(colonnes);

	}

	protected JLabel createTh(String txt)
	{
		JLabel result = new JLabel(txt);
		result.setHorizontalAlignment(SwingConstants.CENTER);
		return result;
	}


	@Override
	public void update(Observable o, Object arg) {

		// TODO Auto-generated method stub

		StudentForm bk = this.studentformdirectoryservice.getStudentForm();

		if (bk == null){
			return;
		}
		else { 
			this.update(bk);
		}


	}

	@Override
	public JComponent getComponentItSelf() {
		// TODO Auto-generated method stub
		return this;
	}

	@Override
	public void update(Object o) {

//		this.removeAll();
//		this.setBackground(Color.white);
//
//		JLabel lbl  = createTh("Year");
//		lbl.setAlignmentX(java.awt.Component.CENTER_ALIGNMENT);
//
//		JPanel panelContenu = new JPanel(new BorderLayout());
//		panelContenu.setBackground(Color.white);
//
//
//		JPanel panelNorth = new JPanel();
//		panelNorth.setBackground(Color.white);
//		panelNorth.add(lbl);
//
//		JPanel panelCenter = new JPanel();
//		panelCenter.setBackground(Color.white);
//
//
//		final String[] columnNames = this.studentformdirectoryservice.getYearTitles();
//
//		final Object[][] data = this.studentformdirectoryservice.getYearData();
//
//
//		// on utilise le tableModel pour empecher que l'utilisateur modifie les donnees lors du click
//		TableModel dataModel = new AbstractTableModel() {
//			public int getColumnCount() { return columnNames.length; }
//			public int getRowCount() { return data.length;}
//			public Object getValueAt(int row, int col) {
//				return data[row][col];
//			}
//			public String getColumnName(int column) {return columnNames[column];}
//			public Class getColumnClass(int col) {
//				return getValueAt(0,col).getClass();
//			}
//			public void setValueAt(Object aValue, int row, int column) {
//				data[row][column] = aValue;
//			}
//		};
//
//
//		table = new JTable(data,columnNames);
//		table.setPreferredScrollableViewportSize(new Dimension(800, 110));
//		table.setFillsViewportHeight(true);
//		table.setForeground(Color.BLACK);
//		table.setRowHeight(25);
//
//		ListSelectionModel listMod = table.getSelectionModel();
//		listMod.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
//
//		listMod.addListSelectionListener(this);
//
//		JScrollPane scrollPane = new JScrollPane();
//		scrollPane.setViewportView(table);
//
//		panelCenter.add(scrollPane);
//
//		panelContenu.add(panelNorth, BorderLayout.NORTH);
//		panelContenu.add(panelCenter, BorderLayout.CENTER);
//
//		this.add(panelContenu, BorderLayout.NORTH);
//
//
//		this.getParent().validate();  // pour rafraichir la page


		String[] columnNames = this.studentformdirectoryservice.getYearTitles();
		
		System.out.println(columnNames);
		System.out.println(columnNames.length);

		yearyeartitle.setText(columnNames[0]);
		yearcodetitle.setText(columnNames[1]);
		yearunivtitle.setText(columnNames[2]);
		yeargradetitle.setText(columnNames[3]);
		
		String[][] data = this.studentformdirectoryservice.getYearData();
		
		for(int k=0; k < data.length; k++)
			for(int l=0; l < data[k].length; l++)
				System.out.println("data["+k+"]["+l+"] = "+data[k][l]);
		
		for(int i = 0; i < data.length; i++)
		{
			if (data[i][0] == null) yearyear.setText("", i);
			else yearyear.setText(data[i][0], i);
			
			if (data[i][1] == null) yearcode.setText("", i);
			else yearcode.setText(data[i][1], i);
			
			if (data[i][2] == null) yearuniv.setText("", i);
			else yearuniv.setText(data[i][2], i);
			
			if (data[i][3] == null) yeargrade.setText("", i);
			else yeargrade.setText(data[i][3], i);
		}
		
		// triche ?
		for(int i = data.length; i < 5; i++)
		{
			yearyear.setText("", i);
			yearcode.setText("", i);
			yearuniv.setText("", i);
			yeargrade.setText("", i);
		}


		
		this.validate();  // pour rafraichir la page
		repaint();
		this.getParent().repaint();  // pour rafraichir la page
		this.getParent().validate();  // pour rafraichir la page
		System.out.println(getSize());
		System.out.println(getParent().getClass());

	}

	public String toString() {
		return "StudentApplicationFormYearPanel";
	}




	@Override
	public org.objectweb.fractal.api.Component getFractalComponent() {
		// TODO Auto-generated method stub
		return this.c;
	}

	@Override
	public String getUid() {
		// TODO Auto-generated method stub
		return this.uid;
	}

	@Override
	public void setFractalComponent(org.objectweb.fractal.api.Component c) {
		// TODO Auto-generated method stub
		this.c = c;
	}

	@Override
	public void setUid(String uid) {
		// TODO Auto-generated method stub
		this.uid = uid;
	}


//	@Override
//	public void valueChanged(ListSelectionEvent listSelectionEvent) {
//		// TODO Auto-generated method stub
//
//		// Retourne vrai si la s�lection est l'objet d'une s�rie de changements.
//		if (!listSelectionEvent.getValueIsAdjusting())
//		{
//			TableModel tm = table.getModel();
//
//			int ligne = table.getSelectedRow();
//
//			valeur = String.valueOf(tm.getValueAt(ligne, 0));
//
//
//			// on envoie le msg que la valeur de yearValue a ete change
//			this.studentformdirectoryservice.setYearValue(valeur);
//
//
//		}
//
//	}


	public String getValeur() {
		return valeur;
	}


	public void setValeur(String valeur) {
		this.valeur = valeur;
	}
	
	
	private void modifyComponent(JComponent c, Dimension d)
	{
		c.setSize(d);
		c.setPreferredSize(d);
		c.setMaximumSize(d);
		c.setMinimumSize(d);
	}

}
